/*
 * @(#)TileSet.java	1.0 Apr 27, 2008
 *
 *	The MIT License
 *
 *	Copyright (c) 2008 Malachi de AElfweald <malachid@gmail.com>
 *
 *	Permission is hereby granted, free of charge, to any person obtaining a copy
 *	of this software and associated documentation files (the "Software"), to deal
 *	in the Software without restriction, including without limitation the rights
 *	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *	copies of the Software, and to permit persons to whom the Software is
 *	furnished to do so, subject to the following conditions:
 *
 *	The above copyright notice and this permission notice shall be included in
 *	all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *	THE SOFTWARE.
 */
package org.eoti.swing.tile;

import javax.imageio.ImageIO;
import java.awt.image.*;
import java.awt.*;
import java.util.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;

public class TileSet<KEY>
{
	protected BufferedImage imgOfTiles;
	protected int rows, cols;
	protected int tileHeight, tileWidth;
	protected HashMap<KEY,Point> tiles;

	public TileSet(File imgOfTiles, int rows, int cols)
	throws IOException
	{
		this(ImageIO.read(imgOfTiles), rows, cols);
	}

	public TileSet(URL imgOfTiles, int rows, int cols)
	throws IOException
	{
		this(ImageIO.read(imgOfTiles), rows, cols);
	}

	public TileSet(BufferedImage imgOfTiles, int rows, int cols)
	{
		this.imgOfTiles = imgOfTiles;
		this.rows = rows;
		this.cols = cols;
		tileHeight = imgOfTiles.getHeight() / rows;
		tileWidth = imgOfTiles.getWidth() / cols;
		tiles = new HashMap<KEY,Point>();
	}

	public void addAll(HashMap<KEY,Point> namesAndPositions)
	{
		tiles.putAll(namesAndPositions);
	}

	public void addTile(KEY key, Point position)
	{
		tiles.put(key, position);
	}

	public void addTile(KEY key, int column, int row)
	{
		tiles.put(key, new Point(column, row));
	}

	public boolean contains(KEY key)
	{
		return tiles.containsKey(key);
	}

	public BufferedImage getTile(KEY key)
	{
		Point tilePoint = tiles.get(key);
		if(tilePoint == null) return null;
		return imgOfTiles.getSubimage(tilePoint.x, tilePoint.y, tileWidth, tileHeight);
	}

	public void drawTile(KEY key, Graphics g, Rectangle bounds, Component imageObserver)
	{
		if(key == null) return;
		if(g == null) return;
		if(bounds == null) return;

		Point tilePoint = tiles.get(key);
		if(tilePoint == null) return;

//		CropImageFilter crop = new CropImageFilter(tilePoint.x, tilePoint.y, tileWidth, tileHeight);
//		((Graphics2D)g).drawImage(imgOfTiles, crop, bounds.x, bounds.y);
		g.drawImage(
			imgOfTiles,
			(int)bounds.getMinX(),				// dest left
			(int)bounds.getMinY(),				// dest top
			(int)bounds.getMaxX(),				// dest right
			(int)bounds.getMaxY(),				// dest bottom
			tilePoint.x,						// src left
			tilePoint.y,						// src top
			tilePoint.x + tileWidth,			// src right
			tilePoint.y + tileHeight,			// src bottom
			imageObserver						// to notify of image updates
		);
	}
}